package com.cxyzy.colortextview

import android.graphics.Color
import android.os.Bundle
import android.text.Html
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setTextByHtml()
        setTextBySpannable()
    }

    private fun setTextByHtml() {
        val str = "默认颜色<font color='#FF0000'>红颜色</font>"
        textView1.text = Html.fromHtml(str)
    }

    private fun setTextBySpannable() {
        val spannableString = SpannableString("默认颜色红颜色")
        val color = Color.parseColor("#FF0000")
        val startIndex = 4
        val endIndex = spannableString.length
        spannableString.setSpan(
            ForegroundColorSpan(color),
            startIndex,
            endIndex,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        textView2.text = spannableString
    }


}
